package org.PDF.Html
{
	/**
	 * To handle HTML entities in String
	 */
	
	public class HtmlEntities
	{		
		private static var _list:Array = new Array(
				{ code:'&quot;', char:'"'},
				{ code:'&apos;', char:'\''},
				{ code:'&amp;', char:'&'},
				{ code:'&lt;', char:'<'},
				{ code:'&gt;', char:'>'},
				{ code:'&nbsp;', char:''},
				{ code:'&iexcl;', char:'¡'},
				{ code:'&cent;', char:'¢'},
				{ code:'&pound;', char:'£'},
				{ code:'&curren;', char:'¤'},
				{ code:'&yen;', char:'¥'},
				{ code:'&brvbar; &brkbar;', char:'¦'},
				{ code:'&sect;', char:'§'},
				{ code:'&um; &die;', char:'¨'},
				{ code:'&copy;', char:'©'},
				{ code:'&ordf;', char:'ª'},
				{ code:'&laquo;', char:'«'},
				{ code:'&not', char:'¬'},
				{ code:'&shy;', char:'­'},
				{ code:'&reg;', char:'®'},
				{ code:'&macr; &hibar;', char:'¯'},
				{ code:'&deg;', char:'°'},
				{ code:'&plusmn;', char:'±'},
				{ code:'&sup2;', char:'²'},
				{ code:'&sup3;', char:'³'},
				{ code:'&acute;', char:'´'},
				{ code:'&micro;', char:'µ'},
				{ code:'&para;', char:'¶'},
				{ code:'&middot;', char:'·'},
				{ code:'&cedil;', char:'¸'},
				{ code:'&sup1;', char:'¹'},
				{ code:'&ordm;', char:'º'},
				{ code:'&raquo;', char:'»'},
				{ code:'&frac14;', char:'¼'},
				{ code:'&frac12;', char:'½'},
				{ code:'&frac34;', char:'¾'},
				{ code:'&iquest;', char:'¿'},
				{ code:'&times;', char:'×'},
				{ code:'&Oslash;', char:'Ø'},
				{ code:'&divide;', char:'÷'},
				{ code:'&oslash;', char:'ø'},
				{ code:'&fnof;', char:'ƒ'},
				{ code:'&circ;', char:'ˆ'},
				{ code:'&tilde;', char:'˜'},
				{ code:'&ndash;', char:'–'},
				{ code:'&mdash;', char:'—'},
				{ code:'&lsquo;', char:'‘'},
				{ code:'&rsquo;', char:'’'},
				{ code:'&sbquo;', char:'‚'},
				{ code:'&ldquo;', char:'“'},
				{ code:'&rdquo;', char:'”'},
				{ code:'&bdquo;', char:'„'},
				{ code:'&dagger;', char:'†'},
				{ code:'&Dagger;', char:'‡'},
				{ code:'&bull;', char:'•'},
				{ code:'&hellip;', char:'…'},
				{ code:'&permil;', char:'‰'},
				{ code:'&lsaquo;', char:'‹'},
				{ code:'&rsaquo;', char:'›'},
				{ code:'&euro; (?)', char:'€'},
				{ code:'&trade;', char:'™'},
				{ code:'&Agrave;', char:'À'},
				{ code:'&Aacute;', char:'Á'},
				{ code:'&Acirc;', char:'Â'},
				{ code:'&Atilde;', char:'Ã'},
				{ code:'&Auml;', char:'Ä'},
				{ code:'&Aring;', char:'Å'},
				{ code:'&AElig;', char:'Æ'},
				{ code:'&Ccedil;', char:'Ç'},
				{ code:'&Egrave;', char:'È'},
				{ code:'&Eacute;', char:'É'},
				{ code:'&Ecirc;', char:'Ê'},
				{ code:'&Euml;', char:'Ë'},
				{ code:'&Igrave;', char:'Ì'},
				{ code:'&Iacute;', char:'Í'},
				{ code:'&Icirc;', char:'Î'},
				{ code:'&Iuml;', char:'Ï'},
				{ code:'&ETH;', char:'Ð'},
				{ code:'&Ntilde;', char:'Ñ'},
				{ code:'&Ograve;', char:'Ò'},
				{ code:'&Oacute;', char:'Ó'},
				{ code:'&Ocirc;', char:'Ô'},
				{ code:'&Otilde;', char:'Õ'},
				{ code:'&Ouml;', char:'Ö'},
				{ code:'&times;', char:'×'},
				{ code:'&Oslash;', char:'Ø'},
				{ code:'&Ugrave;', char:'Ù'},
				{ code:'&Uacute;', char:'Ú'},
				{ code:'&Ucirc;', char:'Û'},
				{ code:'&Uuml;', char:'Ü'},
				{ code:'&Yacute;', char:'Ý'},
				{ code:'&THORN;', char:'Þ'},
				{ code:'&szlig;', char:'ß'},
				{ code:'&agrave;', char:'à'},
				{ code:'&aacute;', char:'á'},
				{ code:'&acirc;', char:'â'},
				{ code:'&atilde;', char:'ã'},
				{ code:'&auml;', char:'ä'},
				{ code:'&aring;', char:'å'},
				{ code:'&aelig;', char:'æ'},
				{ code:'&ccedil;', char:'ç'},
				{ code:'&egrave;', char:'è'},
				{ code:'&eacute;', char:'é'},
				{ code:'&ecirc;', char:'ê'},
				{ code:'&euml;', char:'ë'},
				{ code:'&igrave;', char:'ì'},
				{ code:'&iacute;', char:'í'},
				{ code:'&icirc;', char:'î'},
				{ code:'&iuml;', char:'ï'},
				{ code:'&eth;', char:'ð'},
				{ code:'&ntilde;', char:'ñ'},
				{ code:'&ograve;', char:'ò'},
				{ code:'&oacute;', char:'ó'},
				{ code:'&ocirc;', char:'ô'},
				{ code:'&otilde;', char:'õ'},
				{ code:'&ouml;', char:'ö'},
				{ code:'&divide;', char:'÷'},
				{ code:'&oslash;', char:'ø'},
				{ code:'&ugrave;', char:'ù'},
				{ code:'&uacute;', char:'ú'},
				{ code:'&ucirc;', char:'û'},
				{ code:'&uuml;', char:'ü'},
				{ code:'&yacute;', char:'ý'},
				{ code:'&thorn;', char:'þ'},
				{ code:'&yuml;', char:'ÿ'},
				{ code:'&OElig;', char:'Œ'},
				{ code:'&oelig;', char:'œ'},
				{ code:'&Scaron;', char:'Š'},
				{ code:'&scaron;', char:'š'},
				{ code:'&Yuml;', char:'Ÿ'}
			)
		
		/**
		 * Convert HTML entities to chars in a String
		 * 
		 * @param htmlText Original HTML string
		 * 
		 * @return Converted string
		 * 
		 */
		public function Html2Chars(htmlText:String):String
		{
			for (var i:int = 0; i < _list.length; i++)
			{				
				htmlText = str_replace(_list[i].char, _list[i].code, htmlText); 
			}
			return htmlText;
		}
		
		/**
		 * Convert special chars to HTML entities in a String
		 * 
		 * @param text Original string
		 * 
		 * @return Converted string
		 * 
		 */
		public function Chars2Html(text:String):String
		{
			for (var i:int = 0; i < _list.length; i++)
			{				
				text = str_replace(_list[i].code, _list[i].char, text); 
			}
			return text;
		}		
		
		/**
		 * @private
		 * To replace all occurence of "replace" in a String
		 * 
		 * @param replace_with The replacement substring
		 * @param replace The substring to replace
		 * @param original The main string
		 * 
		 * @return String
		 */
		private function str_replace( replace_with:String, replace:String, original:String ):String
		{
			var array:Array = original.split(replace);
			return array.join(replace_with);
		}

	}
}